﻿using BLL;
using DevExpress.XtraEditors;
using Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools;
using 纺织贸易管理系统;
using 纺织贸易管理系统.新增窗体;
using 纺织贸易管理系统.设置窗体;

namespace 后整理管理系统.入库管理.配件入库
{
    public partial class 配件采购列表 : Form
    {
        public 配件采购列表()
        {
            InitializeComponent();
            uiDatePicker2.Value = DateTime.Now.Date.AddDays(1).AddSeconds(-1);
            uiDatePicker1.Value = uiDatePicker2.Value.AddDays(-QueryTime.间隔);
            gridView1.Columns["rq"].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";//设置时间显示格式
            gridView1.OptionsCustomization.AllowSort = true;
            query();
        }
        private void query()
        {
            try
            {
                this.ShowWaitForm("正在查询，请等待.............");
                var dbcontext = Connect.CreatConnect();
                gridControl1.DataSource = dbcontext.Query($"SELECT * FROM DanjuTable dt  ,ImportAndExportOfRawMaterials iaeorm WHERE dt.dh=iaeorm.DocNum AND " +
                $"dt.rq BETWEEN '{uiDatePicker1.Value.Date}' AND '{uiDatePicker2.Value.Date.AddDays(1)}' AND dt.ksmc like '%{txtkehu.Text}%' AND iaeorm.ProductName" +
                $" LIKE '%{txtpingming.Text}%' and iaeorm.Specifications like '%{txtganghao.Text }%' and dt.djlx='{DanjuLeiXing.配件采购单}'");

            }
            catch (Exception ex)
            {
                ShowMessageBox.ShowErrorBox("查询的时候发生了错误！" + ex.Message);
            }
            finally
            {
                this.HideWaitForm();
            }
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            query();
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("配件入库单") == true)
            {
                MainForm.mainform.AddMidForm(new 配件入库单() { useful = FormUseful.新增 });
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("配件入库单"))
            {
                if ((int)(MessageBox.Show("您确定要删除改配件入库单吗？确定按YES.取消按NO", "提示", MessageBoxButtons.YesNo)) == 6)
                {
                    try
                    {
                        using (Connect.dbcontext.UseTran())
                        {
                            var danhao = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString();
                            配件采购BLL.删除单据(danhao);
                            Connect.dbcontext.CommitTran();
                        }
                    }
                    catch (Exception ex)
                    {
                        ShowMessageBox.ShowErrorBox("删除单据的时候发生了错误:"+ex.Message);
                    }
                    query();
                }
            }
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("修改配件入库单") == true)
            {
                if (单据BLL.检查是否已经审核(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()) == false)
                {
                    MainForm.mainform.AddMidForm(new 配件入库单() { useful = FormUseful.修改, danju = DanjuTableService.GetOneDanjuTable(x => x.dh == gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()) });
                }
                else
                {
                    if (SysInfo.GetInfo.own != "审核制")
                    {
                        MainForm.mainform.AddMidForm(new 配件入库单() { useful = FormUseful.修改, danju = DanjuTableService.GetOneDanjuTable(x => x.dh == gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()) });
                    }
                    else
                    {
                        MessageBox.Show("对不起!改单据已经审核，如需修改请先取消审核状态！", this.Name, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }

        private void 导出到EXCELToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExportFile.导出到文件(gridControl1, "配件采购清单");
        }
    }
}
